package com.laizt.client.macaddr;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.laizt.client.openexe.OpenExeServer;
import com.laizt.client.util.MacAddressUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class MacIpServer {
    private static final Logger logger = LoggerFactory.getLogger(OpenExeServer.class.getName());

    public MacIpServer() {
        new Thread(new MacIpServer.SocketAccept()).start();
    }

    /**
     * 阻塞线程，接收请求
     */
    class SocketAccept implements Runnable {
        public void run(){
            try {
                int port = 8886;
                ServerSocket ss = new ServerSocket(8886,200);
                while(true){
                    logger.info("等待打开获取MAC程序连接请求.....");
                    Socket s = ss.accept();
                    new Thread(new MacIpServer.SocketAction(s)).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 打开本地exe工作线程
     */
    class SocketAction implements Runnable{
        Socket client;
        public SocketAction(Socket client) {
            this.client = client;
        }
        public void run() {
            logger.info("获取本地mac");
            Map<String,String> resultMap = new HashMap<>();
            try {
                InputStream in = client.getInputStream();
                //获取Ip和共享路径，并打开本地exe
                byte[]  b = new byte[2048];
                in.read(b);
                String msg = new String(b,"UTF-8").trim();
                logger.info("获取ip服务端获取到的数据："+msg);
                Map<String,Object> map = (Map<String, Object>) JSONObject.parse(msg);
                logger.info("将要获取的ip:"+map.get("computerIp"));
                resultMap= MacAddressUtil.getMacAddress((String)map.get("computerIp"));
            } catch (Exception e) {
                e.printStackTrace();
                resultMap.put("code","0");
                resultMap.put("msg","获取ip客户端异常");
            }finally {
                logger.info("响应信息:"+ JSONUtils.toJSONString(resultMap));
                try {
                    OutputStreamWriter outSW = null;
                    outSW = new OutputStreamWriter(client.getOutputStream(), "UTF-8");
                    outSW.write(JSONUtils.toJSONString(resultMap));
                    outSW.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                overThis();
            }
        }

        /**
         * 关闭线程
         */
        private void overThis() {
            if(client!=null){
                try {
                    client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
